import {nanoid} from "nanoid";
import {message_list, question_list, task_list} from "src/utils/data";


export function formatTaskList(task_list) {
    let list = []

    task_list.forEach(function (task) {
        if (task.type === 201) {
            // return
            let message = getMessage(task.code)
            list.push({
                code: task.code,
                checkpoint: task.checkpoint,
                type: 100,
                memo: task.memo,
                content: message.content,
                content_css: typeof (message.content_class) === "undefined" ? '' : message.content_class,
                image: message.image,
                image_css: typeof (message.image_css) === "undefined" ? '' : message.image_css,
                front_image: message.front_image,
                front_image_css: typeof (message.front_image_css) === "undefined" ? '' : message.front_image_css,
            })
        } else if (task.type === 202) {
            let message = getQuestion(task.code)
            list.push({
                code: task.code,
                checkpoint: task.checkpoint,
                type: 200,
                memo: task.memo,
                content: message.title,
                content_css: typeof (message.title_class) === "undefined" ? '' : message.title_class,
                image: message.image,
                image_css: typeof (message.image_css) === "undefined" ? '' : message.image_css,
                front_image: message.front_image,
                front_image_css: typeof (message.front_image_css) === "undefined" ? '' : message.front_image_css,
            })

            message.content.forEach(function (item, index) {
                list.push({
                    code: nanoid(),
                    checkpoint: task.checkpoint,
                    type: 201,
                    memo: item,
                    content: item,
                    content_css: '',
                })

            })
        }
    })

    // 保存到临时缓存
    localStorage.setItem('task_list', JSON.stringify(list))

    return list
}

export function getTaskList() {
    return task_list
    // return JSON.parse(localStorage.getItem('task_list'))
}

export function getTaskListByCheckpointID(task_list, id) {
    let list = []

    task_list.forEach(function (task) {
        if (task.checkpoint === id) {
            list.push(task)
        }
    })

    return list
}

export function formatImageCSS(list) {
    list.forEach(function (checkpoint) {
        checkpoint.flow.nodes.forEach(function (node) {
            if (node.type === 'custom') {
                console.log(node, node.data.image_css)
                node.data.image_css = 'bg bg-center'
                console.log(node.data.image_css)
            }
        })
    })
}

function getMessage(code) {
    for (let index = 0; index < message_list.length; index++) {
        if (message_list[index].code === code) {
            return message_list[index]
        }
    }
    return null
}

function getQuestion(code) {
    for (let index = 0; index < question_list.length; index++) {
        if (question_list[index].code === code) {
            return question_list[index]
        }
    }
    return null
}
